/**
 * tarside-ftp -- A simple FTP client, using the ANSI C and GTK+.
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "list_view.h"
void init_list(GtkWidget *list) {
	GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
	GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes("List Items", renderer, "text", 0, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
	GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING);
	gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
	g_object_unref(store);
}
void add_to_list(GtkWidget *list, const gchar *str) {
	GtkListStore* store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));
	GtkTreeIter iter;
	gtk_list_store_append(store, &iter);
	gtk_list_store_set(store, &iter, 0, str, -1);
}
void clean_list(GtkWidget *list) {
	//	GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
	//	GtkTreeIter iter, tmp_iter;
	//	gboolean valid;
	//	valid = gtk_tree_model_get_iter_first(model, &iter);
	//	while (valid) {
	//		tmp_iter = iter;
	//		valid = gtk_tree_model_iter_next(model, &tmp_iter);//先判断是否到结尾
	//		gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
	//	}

	GtkListStore* store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));
	gtk_list_store_clear(store);
}


static void ___init_list(GtkWidget *list) {
	GtkCellRenderer * renderer = gtk_cell_renderer_text_new();
	GtkTreeViewColumn * column = gtk_tree_view_column_new_with_attributes("List Item", renderer, "text", 0, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
	GtkListStore * store = gtk_list_store_new(1, G_TYPE_STRING);
	gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
	g_object_unref(store);
}


static _init_list_view(GtkWidget * window, GtkWidget *list) {
	gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
	GtkWidget *scrolledWindow = gtk_scrolled_window_new(NULL, NULL);/*创建滚动窗口构件*/
	gtk_widget_set_size_request(scrolledWindow, 700, 250);
	gtk_widget_show(scrolledWindow);
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledWindow), list);
	gtk_container_add(GTK_CONTAINER(window), scrolledWindow);
}



void init_list_view(GtkWidget * window, GtkWidget *list, OnListSelected on_changed, OnDoubleClick on_double_click) {
	_init_list_view(window, list);
	GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
	g_signal_connect(selection, "changed", G_CALLBACK(on_changed), NULL);
	g_signal_connect_swapped(G_OBJECT(list), "event", G_CALLBACK(on_double_click), selection);

}

